package com.ys.malini.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ys.malini.dao.HibernatePagerManager;
import com.ys.malini.dao.ICustomerServiceDao;
import com.ys.malini.model.TServiceinfo;
import com.ys.malini.web.form.CustomerServiceForm;

public class CustomerServiceDao  extends HibernateDaoSupport implements ICustomerServiceDao{

	public CustomerServiceDao(){
		
	}
	
	/**
	 * 新增客服信息
	 */
	public String addCustomserService(CustomerServiceForm formbean) {
		String message="新增失败";

		TServiceinfo customerinfo = new TServiceinfo();

		try {
			customerinfo.setServiceType(formbean.getServiceType());
			customerinfo.setContent(formbean.getContent());
			customerinfo.setRsv1(formbean.getRsv1());
			getSession().save(customerinfo);
			message="新增成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return message;
	}
	
	/**
	 * 删除客服信息
	 */
	public String deleteCustomserService(Map map) {
		String message="删除失败";
		String serviceId=(String) map.get("serviceId");
		String serviceType= (String) map.get("serviceType");
		StringBuffer hql = new StringBuffer(" delete TServiceinfo o where 1=1  and serviceId='"+serviceId+"' and serviceType='"+serviceType+"'");
		
		Query query = getSession().createQuery(hql.toString());		
		query.executeUpdate();
		message="删除成功！";
		return message;
	}
	
	/**
	 * 查询客服信息
	 */
	public List<TServiceinfo> query(CustomerServiceForm form, boolean isPager) {
		System.out.println(".................");
		String hsql = " from TServiceinfo order by  serviceId desc";
		System.out.println("-----" + hsql);
		List<TServiceinfo> customerlist = null;
		if (isPager) {
			customerlist = HibernatePagerManager.searchPaginated(getSession(),
					hsql.toString());
		} else {
			customerlist = getHibernateTemplate().find(hsql);
		}

		return customerlist;
	}

	/**
	 * 查询单个客服信息
	 */
	public TServiceinfo queryCustomserServiceById(String id) {
		String hsql=" from TServiceinfo where serviceId='"+id+"'";
		List list = getHibernateTemplate().find(hsql);
		TServiceinfo customerinfo = list!=null&&list.size()>0?(TServiceinfo)list.get(0):null;
		return customerinfo;
	}
	
	/**
	 * 更新客服信息
	 */
	public String updateCustomserServiceById(CustomerServiceForm formbean) {
		String id = formbean.getServiceId().toString();
		String hsql=" from TServiceinfo where serviceId='"+id+"'";
		TServiceinfo customerinfo = (TServiceinfo) getHibernateTemplate().find(hsql).get(0);
		String message="更新失败";
		try {
			System.out.println("rsv1===33="+formbean.getRsv1());
			customerinfo.setServiceType(formbean.getServiceType());
			customerinfo.setContent(formbean.getContent());
			customerinfo.setRsv1(formbean.getRsv1());
			//BeanUtils.copyProperties(formbean,customerinfo);
			getSession().merge(customerinfo);
			message="更新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	//	customerinfo.setServiceType(formbean.getServiceType());
	//	customerinfo.setContent(formbean.getContent());
		
		return message;
	}

	/**
	 * 客户服务信息前台
	 */
	public TServiceinfo queryTop(String serviceType) {
		String hsql=" from TServiceinfo where serviceType='"+serviceType+"' order by serviceId desc";
		
		Query query = getSession().createQuery(hsql);
		query.setFirstResult(0);
		query.setMaxResults(1);
		List list =query.list();
		TServiceinfo customerinfo =list!=null&&list.size()>0?(TServiceinfo)list.get(0):null;

		return customerinfo;
	}
	

	

}
